package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;
    //查询相似度最高
    public RecommendUser findMaxScore(Long toUserId) {
        //创建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);

        //根据Criteria 创建 Query
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        //查询
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        return user;
    }



    //查询推荐人信息
    public List<RecommendUser> findRecommendUserList(Long toUserId, Integer page, Integer pageSize) {
        //创建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);

        //根据Criteria 创建 Query
        //查询总数
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, RecommendUser.class);
        System.out.println("总数：   "+count);
        //分页查询
        query.with(Sort.by(Sort.Order.desc("score"))).limit(pageSize).skip((page-1) * pageSize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        return list;
    }

    //查询推荐信息
    public RecommendUser findByUserId(Long toUserId, Long userId) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId).and("userId").is(userId);
        Query query = Query.query(criteria);
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        return user;
    }



}
